
package com.rt.schedulenew.web;

import com.rt.schedulebase.dto.*;
import com.rt.schedulebase.entity.*;
import com.rt.schedulenew.api.IDictExamQueueService;
import com.rt.schedulenew.api.IDictScheduleApmService;
import com.rt.schedulenew.api.IDictSysparamsService;
import com.rt.schedulenew.api.IExamScheduleService;
import com.rt.schedulenew.api.*;
import com.rt.schedulenew.utils.base.NoticeDto;
import com.rt.schedulenew.utils.checkparam.config.Check;
import com.rt.schedulenew.utils.global.BusinessException;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.util.JsonUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping({"/dict"})
public class DictController {
    @Autowired
    private ITokenService tokenService;
    @Autowired
    private IDictDeptService dictDeptService;
    @Autowired
    private IDictExamClassService dictExamClassService;
    @Autowired
    private IDictExamQueueService dictExamQueueService;
    @Autowired
    private IDictHospitalService dictHospitalService;
    @Autowired
    private IDictWardService dictWardService;
    @Autowired
    private IDictPatientSourceService dictPatientSourceService;
    @Autowired
    private IDictExamItemService dictExamItemService;
    @Autowired
    private IDictExamSubclassService dictExamSubclassService;
    @Autowired
    private IDictItemGroupService dictItemGroupService;
    @Autowired
    private IDictItemGroupDetailService dictItemGroupDetailService;
    @Autowired
    private IDictPrintPatternService dictPrintPatternService;
    @Autowired
    private IDictCommonUseService dictCommonUseService;
    @Autowired
    private IDictSmsContentService dictSmsContentService;
    @Autowired
    private IDictWorkDateService dictWorkDateService;
    @Autowired
    private IDictQueueClassService dictQueueClassService;
    @Autowired
    private IDictSexService dictSexService;
    @Autowired
    private IDictSysparamsService sysparamsService;
    @Autowired
    private IDictScheduleApmService dictScheduleApmService;
    @Autowired
    private IExamScheduleService examScheduleService;
    @Autowired
    private IDictUsersService dictUsersService;
    @Autowired
    private IDictExamZoneService dictExamZoneService;

    @PostMapping({"/dept/list"})
    public JsonResult getDeptList(@RequestBody DictDeptDto dto) {
        return JsonResultUtil.success(dictDeptService.getList(dto));
    }

    /***
     * 获取-检查队列
     * @param req
     * @return
     */
    @PostMapping({"/examQueue/deptForQueueNameList"})
    public JsonResult deptForQueueNameList(@RequestBody DictExamQueueDto req) {
        List<DictExamQueueDto> returnList = new ArrayList<>();
        if (req.getExamClasss() != null && req.getExamClasss().size() > 0) {
            List<DictExamQueueDto> finalReturnList = returnList;
            req.getExamClasss().forEach(item -> {
                DictExamQueueDto dictExamQueueDto = new DictExamQueueDto();
                try {
                    BeanUtils.copyProperties(req,dictExamQueueDto);
                } catch (Exception e) {
                    throw new BusinessException("数据解析失败！");
                }
                dictExamQueueDto.setExamClass(item);
                List<DictExamQueueDto> dictExamQueueDtos = dictExamQueueService.deptForQueueNameList(dictExamQueueDto);
                finalReturnList.addAll(dictExamQueueDtos);
            });
        } else {
            returnList = dictExamQueueService.deptForQueueNameList(req);
        }
        return JsonResultUtil.success("成功", returnList);
    }

    @PostMapping({"/ward/list"})
    public JsonResult getWardList(@RequestBody DictWardDto dto) {
        return JsonResultUtil.success(dictWardService.getList(dto));
    }

    @PostMapping({"/ward/wardList"})
    public JsonResult getWardForDept(@RequestBody DictWardDto dto) {
        return JsonResultUtil.success(dictWardService.getWardList(dto));
    }

    /***
     * 获取可以预约院区列表
     * @param dto
     * @return
     */
    @PostMapping({"/hospital/list"})
    public JsonResult getHospitalList(@RequestBody DictHospitalDto dto) {
        return JsonResultUtil.success("成功", dictHospitalService.getHospitalList(dto));
    }

    @PostMapping({"/hospital/list/all"})
    public JsonResult getHospitalListAll(@RequestBody DictHospitalDto dto) {
        return JsonResultUtil.success("成功", dictHospitalService.getHospitalListAll(dto));
    }

    @PostMapping({"/examQueue/list"})
    public JsonResult getExamQueueList(@RequestBody DictExamQueueDto dictExamQueueDto) {
        if (StringUtils.isBlank(dictExamQueueDto.getExamClass())) {
            return JsonResultUtil.success();
        }
        return dictExamQueueService.getQueueListPage(dictExamQueueDto);
    }

    @PostMapping({"/patientSource/list"})
    public JsonResult getPatientSourceList() {
        return JsonResultUtil.success(dictPatientSourceService.getListAll());
    }

    @PostMapping({"/examClass/list"})
    public JsonResult getExamClassList(@RequestBody DictExamClassDto dto) {
        dto.setExamClassName(dto.getExamClass());
        dto.setClassAttr("0");
        List<DictExamClass> list = dictExamClassService.getExamClassList(dto);
        return JsonResultUtil.success(list);
    }

    @PostMapping({"/examQueue/insert"})
    public JsonResult insertNewExamQueue(@RequestBody DictExamQueueDto dto) {
        return dictExamQueueService.insertNewExamQueue(dto);
    }

    @Transactional(rollbackFor = {Exception.class})
    @PostMapping({"/examQueue/update"})
    public JsonResult updateExamQueue(@RequestBody DictExamQueueDto examQueueDto) {
        return dictExamQueueService.updateExamQueue(examQueueDto);
    }

    /**
     * 删除预约队列字典表
     *
     * @param dto
     * @return
     */
    @PostMapping({"/examQueue/delete"})
    public JsonResult deleteExamQueue(@RequestBody DictExamQueueDto dto) {
        return dictExamQueueService.deleteExamQueue(dto);
    }

    @PostMapping({"/examItem/update"})
    public JsonResult updateExamItem(@RequestBody List<DictExamItemDto> list, HttpServletRequest req) {
        DictUsersDto dictUsers = tokenService.getCurrentUser(req);
        dictExamItemService.updateBatch(list, dictUsers);
        return JsonResultUtil.success();
    }

    @PostMapping({"/itemNotice/insert"})
    @Check({"itemName", "examClass", "examSubClass"})
    public JsonResult itemNotice(@RequestBody DictExamItemDto dto) {
        return null;
    }

    @PostMapping({"/itemNotice/noticeList"})
    @Check({"itemName", "examClass", "examSubClass"})
    public JsonResult noticeList(@RequestBody DictExamItemDto dto) {
        return null;
    }

    @PostMapping({"/itemNotice/update"})
    @Check({"itemName", "examClass", "examSubClass", "sort"})
    public JsonResult updateNotice(@RequestBody DictExamItemDto dto) {
        return null;
    }

    @PostMapping({"/itemNotice/delete"})
    @Check({"itemName", "examClass", "examSubClass", "sort"})
    public JsonResult deleteNotice(@RequestBody DictExamItemDto dto) {
        return null;
    }

    @PostMapping({"/examItem/list"})
    public JsonResult getExamItemList(@RequestBody DictExamItemDto dto) {
        return dictExamItemService.getListPage(dto);
    }

    @GetMapping({"/examItem/downloadTemplate"})
    public void getExamItemTemplate(HttpServletResponse response) {
        dictExamItemService.getTemplate(response);
    }

    @PostMapping({"/examItem/batchImport"})
    public JsonResult batchImportExamItem(MultipartFile file) {
        return dictExamItemService.batchImportExamItem(file);
    }

    /**
     * 加载检查类别的子类
     *
     * @param dto
     * @return
     */
    @PostMapping({"/examSubClass/list"})
    public JsonResult getExamSubClass(@RequestBody DictExamSubclassDto dto) {
        if (StringUtils.isBlank(dto.getExamClassName())) {
            return JsonResultUtil.success(Arrays.asList());
        }
        List<DictExamSubclass> list = dictExamSubclassService.getList(dto);
        return JsonResultUtil.success(list);
    }

    /**
     * 查询检查项目分组列表
     *
     * @param dto
     * @return
     */
    @PostMapping({"/itemGroup/list"})
    public JsonResult getItemGroup(@RequestBody DictItemGroupDto dto) {
        return dictItemGroupService.getItemGroupList(dto);
    }

    @PostMapping({"/itemPackage/list"})
    public JsonResult getItemPackage(@RequestBody DictItemGroupDto dto) {
        if(Objects.isNull(dto.getPageNo())){
            dto.setPageNo(1);
        }
        if(Objects.isNull(dto.getPageSize())){
            dto.setPageSize(Integer.MAX_VALUE);
        }
        return dictItemGroupService.getItemPackageList(dto);
    }

    @PostMapping({"/itemGroup/insert"})
    public JsonResult insertNewItemGroup(@RequestBody DictItemGroupDto dto) {
        return dictItemGroupService.insertNewItemGroup(dto);
    }

    @PostMapping({"/itemGroup/delete"})
    public JsonResult deleteItemGroup(@RequestBody DictItemGroupDto dto) {
        return dictItemGroupService.deleteItemGroup(dto);
    }

    @PostMapping({"/itemGroup/update"})
    public JsonResult updateItemGroup(@RequestBody DictItemGroupDto dto) {
        return dictItemGroupService.updateItemGroup(dto);
    }

    @PostMapping({"/itemGroupDetail/list"})
    public JsonResult getItemGroupDetailList(@RequestBody DictItemGroupDetailDto dto) {
        return dictItemGroupDetailService.getItemGroupDetailList(dto);
    }

    /**
     * 可以插入的分组列表
     * @param dto
     * @return
     */
    @PostMapping({"/itemGroupDetail/insertList"})
    public JsonResult groupDetailListCanInsert(@RequestBody DictExamItemDto dto) {
        return dictExamItemService.getListPage(dto);
    }

    @PostMapping({"/itemGroupDetail/insert"})
    public JsonResult insertGroupDetail(@RequestBody List<DictItemGroupDetailDto> list) {
        return dictItemGroupDetailService.insertGroupDetail(list);
    }

    @PostMapping({"/itemGroupDetail/update"})
    public JsonResult updateGroupDetail(@RequestBody DictItemGroupDetailDto dto) {
        return dictItemGroupDetailService.updateGroupDetail(dto);
    }

    @PostMapping({"/itemGroupDetail/delete"})
    public JsonResult deleteItemGroupDetail(@RequestBody List<DictItemGroupDetailDto> dtoList) {
        return dictItemGroupDetailService.deleteItemGroupDetail((List) dtoList);
    }

    @PostMapping({"/pattern/list"})
    public JsonResult getPrintPattern(@RequestBody DictPrintPatternDto dto) {
        List<DictPrintPatternDto> list = dictPrintPatternService.getPatternList(dto);
        return JsonResultUtil.success(list);
    }

    @PostMapping({"/pattern/insert"})
    @Check({"patternContent", "patternType", "terminalType"})
    public JsonResult insertPrintPattern(@RequestBody DictPrintPatternDto dto) {
        return dictPrintPatternService.insert(dto);
    }

    @PostMapping({"/pattern/update"})
    @Check({"patternId"})
    public JsonResult updatePrintPattern(@RequestBody DictPrintPatternDto dto) {
        return dictPrintPatternService.updatePattern(dto);
    }

    @PostMapping({"/pattern/delete"})
    @Check({"patternId"})
    public JsonResult delPrintPattern(@RequestBody DictPrintPatternDto dto) {
        return dictPrintPatternService.delPattern(dto);
    }

    /**
     * 通用字典表
     *
     * @param dto
     * @return
     */
    @PostMapping({"/commonUse/list"})
    public JsonResult getCommonUse(@RequestBody DictCommonUseDto dto) {
        return dictCommonUseService.getListPage(dto);
    }

    @PostMapping({"/commonUse/insert"})
    public JsonResult insertCommonUse(@RequestBody DictCommonUseDto dto) {
        return dictCommonUseService.insertCommonUse(dto);
    }

    @PostMapping({"/commonUse/delete"})
    @Check({"name", "value"})
    public JsonResult deleteCommonUse(@RequestBody DictCommonUseDto dto) {
        dictCommonUseService.deleteByDto(dto);
        return JsonResultUtil.success();
    }

    @PostMapping({"/commonUse/update"})
    @Check({"oldName", "oldExamClass", "oldReserve1"})
    public JsonResult updateCommonUse(@RequestBody DictCommonUseDto dto) {
        dictCommonUseService.updateByDto(dto);
        return JsonResultUtil.success();
    }

    /**
     * 获取护理单元列表
     * @return
     */
    @PostMapping({"/dept/nurseUnitList"})
    public JsonResult selectNurseUnit() {
        return JsonResultUtil.success(dictDeptService.getNurseUnit());
    }

    @PostMapping({"/smsContent/insert"})
    @Check({"smsContent", "smsType"})
    public JsonResult insertSmsContentInfo(@RequestBody DictSmsContent dictSmsContent) {
        dictSmsContentService.insertSelective(dictSmsContent);
        return JsonResultUtil.success();
    }

    @PostMapping({"/smsContent/delete"})
    @Check({"smsId"})
    public JsonResult deleteSmsContent(@RequestBody DictSmsContentDto dto) {
        dictSmsContentService.deleteByPrimaryKey(dto.getSmsId());
        return JsonResultUtil.success();
    }

    @PostMapping({"/smsContent/update"})
    @Check({"smsId"})
    public JsonResult updateSmsContent(@RequestBody DictSmsContent dictSmsContent) {
        dictSmsContentService.updateByPrimaryKeySelective(dictSmsContent);
        return JsonResultUtil.success();
    }

    @PostMapping({"/smsContent/list"})
    public JsonResult findSmsContent(@RequestBody DictSmsContentDto dto) {
        return JsonResultUtil.success("成功", dictSmsContentService.getList(dto));
    }

    @PostMapping({"/workDate/list"})
    public JsonResult getWorkDateList(@RequestBody DictWorkDateDto dto) {
        return JsonResultUtil.success(dictWorkDateService.getList(dto));
    }

    @PostMapping({"/workDate/insert"})
    public JsonResult insertWorkDate(@RequestBody DictWorkDate dictWorkDate) {
        if (dictWorkDate.getMemo() != null && dictWorkDate.getMemo().length() > 128) {
            return JsonResultUtil.failure("备注超长！");
        }
        dictWorkDateService.insertSelective(dictWorkDate);
        return JsonResultUtil.success();
    }

    @PostMapping({"/workDate/update"})
    public JsonResult updateWorkDate(@RequestBody DictWorkDateDto dto) {
        if (dto.getMemo() != null && dto.getMemo().length() > 128) {
            return JsonResultUtil.failure("备注超长！");
        }
        dictWorkDateService.updateBatch(dto);
        return JsonResultUtil.success();
    }

    @PostMapping({"/workDate/delete"})
    public JsonResult deleteWorkDate(@RequestBody DictWorkDate entity) {
        dictWorkDateService.delete(entity);
        return JsonResultUtil.success();
    }

    @PostMapping({"/workDate/insert/auto"})
    public JsonResult workDateInsertAuto(@RequestBody DictWorkDateDto dto) {
        dictWorkDateService.insertAuto(dto);
        return JsonResultUtil.success();
    }

    @PostMapping({"/priority/find"})
    public JsonResult findPriority(@RequestBody DictQueueClassDto dto) {
        return JsonResultUtil.success(dictQueueClassService.getList(dto));
    }

    @PostMapping({"/hospital/insert"})
    public JsonResult insertHospital(@RequestBody DictHospitalDto dto) {
        if (StringUtils.isEmpty(dto.getHospitalName())
                || (StringUtils.isNotBlank(dto.getHospitalName()) && dto.getHospitalName().length() > 24)) {
            return JsonResultUtil.failure("参数错误:院区名称太长!");
        }
        return JsonResultUtil.success("成功", dictHospitalService.insertHospital(dto));
    }

    @PostMapping({"/hospital/update"})
    public JsonResult updateHospital(@RequestBody DictHospitalDto dto) {
        if (StringUtils.isEmpty(dto.getHospitalName())
                || (StringUtils.isNotBlank(dto.getHospitalName()) && dto.getHospitalName().length() > 24)) {
            return JsonResultUtil.failure("参数错误:院区名称太长!");
        }
        return JsonResultUtil.success("成功", dictHospitalService.updateHospital(dto));
    }

    @PostMapping({"/hospital/delete"})
    public JsonResult deleteHospital(@RequestBody DictHospitalDto dto) {
        return JsonResultUtil.success("成功", dictHospitalService.deleteHospital(dto));
    }

    @PostMapping({"/sex/list"})
    public JsonResult getSexList(@RequestBody DictSex dto) {
        return JsonResultUtil.success("成功", dictSexService.getListAll());
    }

    @PostMapping({"/transTool/list"})
    public JsonResult getTransToolList() {
        return sysparamsService.getTransToolList();
    }

    @PostMapping({"/user/list"})
    public JsonResult getUserList(@RequestBody DictUsersDto dto) {
        return JsonResultUtil.success(dictUsersService.getList(dto));
    }

    @PostMapping({"/getScheduleApm/list"})
    public JsonResult getScheduleApm(@RequestBody DictScheduleApmDto dto) {
        return JsonResultUtil.success("成功", dictScheduleApmService.getScheduleApm(dto));
    }

    /**
     * 报到率统计
     * @param dto
     * @return
     */
    @PostMapping({"/checkRateCount"})
    public JsonResult checkRateCount(@RequestBody ExamScheduleDto dto) {
        Map<String, Object> map = examScheduleService.checkRateCount(dto);
        return JsonResultUtil.success("成功", map);
    }

    /**
     * 获取检查类型的检查区域
     *
     * @param dto
     * @return
     */
    @PostMapping({"/examZone/list"})
    public JsonResult getExamZoneList(@RequestBody DictExamZoneDto dto) {
        return JsonResultUtil.success(dictExamZoneService.getList(dto));
    }

    @PostMapping({"/examZone/insert"})
    public JsonResult insertExamZone(@RequestBody DictExamZoneDto dto) {
        return JsonResultUtil.success(dictExamZoneService.insertSelective(dto));
    }

    @PostMapping({"/examZone/update"})
    public JsonResult updateExamZone(@RequestBody DictExamZoneDto dto) {
        return JsonResultUtil.success(dictExamZoneService.update((DictExamZone) dto));
    }

    @PostMapping({"/examZone/delete"})
    public JsonResult deleteExamZone(@RequestBody DictExamZoneDto dto) {
        return JsonResultUtil.success(dictExamZoneService.delete((DictExamZone) dto));
    }

    @PostMapping({"/examItem/noticeList"})
    @Check({"itemName", "examClass", "examSubClass", "itemCode", "noticeType"})
    public JsonResult notices(@RequestBody DictExamItemDto apm) {
        List<NoticeDto> noticeList = dictExamItemService.getNoticeList(apm);
        return JsonResultUtil.success(noticeList);
    }

    @PostMapping({"/examItem/noticeUpdate"})
    @Check({"itemName", "examClass", "examSubClass", "itemCode", "operateTag", "noticeType"})
    public JsonResult noticeSave(@RequestBody DictExamItemDto apm) {
        Boolean aBoolean = dictExamItemService.saveOrUpdateNotice(apm);
        if (aBoolean) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }

    @PostMapping({"/examItem/mobileNotice"})
    @Check({"itemCode"})
    public JsonResult mobileNotice(@RequestBody DictExamItemDto apm) {
        List<DictExamItemMobileNoticeItemDto> noticeList = dictExamItemService.getMobileNoticeList(apm);
        return JsonResultUtil.success(noticeList);
    }

    @PostMapping({"/examItem/modifyMobileNotice"})
    @Check({"itemCode"})
    public JsonResult modifyMobileNotice(@RequestBody DictExamItemDto apm) {
        List<DictExamItemMobileNoticeItemDto> noticeItemDtos = JsonUtil.toList(apm.getMobileNotice(), DictExamItemMobileNoticeItemDto.class);
        boolean flag = dictExamItemService.saveMobileNoticeList(apm.getItemCode(), noticeItemDtos);
        if (flag) {
            return JsonResultUtil.success();
        } else {
            return JsonResultUtil.failure();
        }
    }

    @PostMapping({"/examItem/noticeBatchSave"})
    public JsonResult noticeBatchSave(@RequestBody List<DictExamItemDto> dtoList) {
        Boolean aBoolean = dictExamItemService.noticeBatchSave(dtoList);
        if (aBoolean) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }
}
